package cn.openea.perpartner.meily.web.entity.dto;

import cn.openea.perpartner.common.core.annotation.Excel;
import cn.openea.perpartner.common.core.utils.StringUtils;
import cn.openea.perpartner.meily.web.entity.Enterprise;
import cn.openea.perpartner.meily.web.entity.Goods;
import cn.openea.perpartner.meily.web.entity.LearningCourse;
import cn.openea.perpartner.meily.web.service.IGoodsService;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author ysk
 * @version 1.0.1
 * @Description 企业DTO工厂
 * @createTime 2023年06月27日 23:29:00
 */
@Component
public class EnterpriseDtoFactory {

    @Resource
    private IGoodsService goodsService;


    /***
     * 企业关联项目DTO
     */
    @Data
    @SuperBuilder
    @AllArgsConstructor
    @NoArgsConstructor
    public static class RelevancyItemQuantityDto {
        /**
         * 关联商品数量
         */
        @Builder.Default
        private Integer goodsQuantity = 0;
        /**
         * 关联品牌数量
         */
        @Builder.Default
        private Integer brandQuantity = 0;
        /**
         * 关联品牌
         */
        private List<String> brandList;
        /**
         * 关联招聘岗位数量
         */
        @Builder.Default
        private Integer recruitmentQuantity = 0;
        /**
         * 关联课程数量
         */
        @Builder.Default
        private Integer courseQuantity = 0;
    }

    public Function<Enterprise, RelevancyItemQuantityDto> toRelevancyItemQuantityDto = (enterprise) -> {
        if (enterprise == null) {
            return null;
        }
        List<String> brandList = goodsService.selectBrandListByEnterpriseNo(enterprise.getEnterpriseNo());
        return RelevancyItemQuantityDto.builder()
                .goodsQuantity(goodsService.countGoodsByEnterpriseNo(enterprise.getEnterpriseNo()))
                .brandList(brandList)
                .brandQuantity(brandList != null ? brandList.size() : 0)
                .build();
    };

    /***
     * 企业关联项目DTO
     */
    @Data
    @SuperBuilder
    @AllArgsConstructor
    @NoArgsConstructor
    public static class RelevancyItemDto {
        /**
         * 关联商品数量
         */
        private List<Goods> goodsList;
        /**
         * 关联商品品牌
         */
        private List<String> brandList;
        /**
         * 关联招聘岗位
         */
        private List<JobDto> jobDtoList;
        /**
         * 关联课程
         */
        private List<LearningCourse> courseList;
    }

    public Function<Enterprise, RelevancyItemDto> toRelevancyItemDtoDto = (enterprise) -> {
        if (enterprise == null) {
            return null;
        }
        List<Goods> goodsList = goodsService.selectListByEnterpriseNo(enterprise.getEnterpriseNo());
        List<String> brandList = goodsList.parallelStream()
                .map(Goods::getBrand)
                .filter(StringUtils::isNotBlank)
                .distinct()
                .collect(Collectors.toList());

        return RelevancyItemDto.builder()
                .goodsList(goodsList)
                .brandList(brandList)
                .build();
    };

    /**
     * 企业详情DTO
     */
    @Data
    @SuperBuilder
    @AllArgsConstructor
    @NoArgsConstructor
    public static class IntegratedDetailsDto {
        /**
         * 企业信息
         */
        private Enterprise enterpriseInfo;
        /**
         * 企业关联项目信息
         */
        private RelevancyItemDto relevancyItemDto;
    }

    public Function<Enterprise, IntegratedDetailsDto> toIntegratedDetailsDto = (enterprise) -> {
        if (enterprise == null) {
            return null;
        }
        return IntegratedDetailsDto.builder()
                .enterpriseInfo(enterprise)
                .relevancyItemDto(toRelevancyItemDtoDto.apply(enterprise))
                .build();
    };

    /**
     * 导入导出DTO
     */
    @Data
    @SuperBuilder
    @AllArgsConstructor
    @NoArgsConstructor
    public static class ImportTemplateDto {
        @Excel(name = "企业名称")
        private String enterpriseName;
        @Excel(name = "企业简介")
        private String introduction;
        @Excel(name = "统一社会信用代码")
        private String organizationCode;
        @Excel(name = "企业类型")
        private String registrationType;
        @Excel(name = "所属行业")
        private String industry;
        @Excel(name = "所属省份")
        private String province;
        @Excel(name = "所属城市")
        private String city;
        @Excel(name = "所属区县")
        private String area;
        @Excel(name = "成立日期")
        private Date registerDate;
        @Excel(name = "注册地址")
        private String registeredAddress;
        @Excel(name = "注册资本")
        private String registeredCapital;
        @Excel(name = "法定代表人")
        private String legalPerson;
        @Excel(name = "联系电话")
        private String phoneNumber;
        @Excel(name = "电子邮箱")
        private String email;
    }


}
